<?php
namespace app\index\controller;
use think\Cookie;

class Ajax extends Common
{
    public function login()
    {
        if(request()->isPost()){
            $user = input("post.user");
            $pwd = input("post.pwd");
            if(strlen($user) < 3 || strlen($pwd) < 5){
                $this->assign('sweetAlert',sweetAlert('温馨提示','用户名或者密码格式不正确！','3','REFERER'));
            }elseif($row = $this->pdo->find('select * from smalls_user where (user=:user or qq=:user) and pwd=:pwd limit 1', array(':user' => $user, ':pwd' => getPwd($pwd)))){
                if($row['status']==0){
                    $this->assign( 'sweetAlert', sweetAlert( '账号没有激活', '激活邮件已经发到：'.$row['mail'], '3', url('index/sendValidateEmail') . '?email=' . $row['mail']));
                }else {
                    $sid = getPwd( 'www.16145.com' . getPwd($pwd));
                    Cookie::set('smalls_userId', $row['uid'],7200);
                    Cookie::set('smalls_userSid', $sid,7200);
                    $this->assign( 'sweetAlert', sweetAlert( '登录成功', '您已经登录，还是为你转跳到主页面！', '1',url('index/index')));
                }
            }else{
                $this->assign('sweetAlert',sweetAlert('登录失败','您输入的账号或密码不正确！','2','REFERER'));
            }
            exit($this->fetch('common/sweetalert'));
        }
    }
    public function reg()
    {
        @session_start();
        if(request()->isPost()){
            $user = input("post.user");
            $pwd = input("post.pwd");
            $qq = input("post.qq");
            $mail = input("post.mail");
            $code = input("post.code");
            $sid = createSid();
            if (strlen($code) != 5 || $_SESSION['vc_code'] != $code) {
                $this->assign('sweetAlert',sweetAlert('验证码错误','您输入的验证码错误！','2','REFERER'));
            } elseif (strlen($qq) < 5 || strlen($qq) > 12) {
                $this->assign("sweetAlert", sweetAlert("温馨提示", "QQ号码不正确！", "2",'REFERER'));
            } elseif (strlen($user) < 3 || strlen($user) > 12) {
                $this->assign("sweetAlert", sweetAlert("温馨提示", "用户名不能少于3个字符或大于12个字符！", "2",'REFERER'));
            } elseif (strlen($pwd) < 6 || strlen($pwd) > 16) {
                $this->assign("sweetAlert", sweetAlert("温馨提示", "密码不能少于6个字符或大于16个字符！", "2",'REFERER'));
            } elseif ($this->pdo->find('select uid from smalls_user where user=:user limit 1', array(':user' => $user))) {
                $this->assign("sweetAlert", sweetAlert("温馨提示", "此用户名已存在！", "3",'REFERER'));
            } elseif ($this->pdo->find('select uid from smalls_user where qq=:qq where limit 1', array(':qq' => $qq))) {
                $this->assign("sweetAlert", sweetAlert("温馨提示", "此QQ已经注册过！", "3",'REFERER'));
            } elseif ($this->pdo->find('select uid from smalls_user where mail=:mail where limit 1', array(':mail' => $mail))) {
                $this->assign("sweetAlert", sweetAlert("温馨提示", "此邮箱已经注册过！", "3",'REFERER'));
            }elseif ($this->pdo->execute("INSERT INTO `smalls_user` (`user`, `pwd`, `qq`, `mail`, `money`, `sid`, `status`) VALUES (:user, :pwd, :qq, :mail, '0', :sid, '0')", array(':user' => $user, ':pwd' => getPwd($pwd), ':qq' => $qq, ':mail' => $mail, ':sid' => $sid))){
                $url = "http://{$_SERVER['HTTP_HOST']}" . url('index/validateEmail', ['sid' => $sid]);
                $body = "尊敬的用户：<br>您好，感谢你注册" . config('web_name') . "用户。<br>请点击下面链接进行邮箱验证：<a href='{$url}' target='_blank'>{$url}</a><br>邮箱验证后，你就可以开始使用平台了！";
                $mail = sendEmail($mail, "欢迎您注册" . config('web_name'), $body);
                if ($mail->send()) {
                    $this->assign('sweetAlert',sweetAlert('注册成功','请先登录邮箱进行激活账号','1',url('index/login')));
                } else {
                    $this->assign('sweetAlert',sweetAlert('注册成功','邮件发送失败：' . $mail->ErrorInfo,'2','REFERER'));
                }
            }
            unset($_SESSION['vc_code']);
            exit($this->fetch('common/sweetalert'));
        }
    }
    public function password()
    {
        session_start();
        if(request()->isPost()){
            $user = input("post.user");
            $code = input("post.code");
            if (strlen($code) != 5 || $_SESSION['vc_code'] != $code) {
                $this->assign('sweetAlert',sweetAlert('验证码错误','您输入的验证码错误！','2','REFERER'));
            }elseif($row = $this->pdo->find('select * from smalls_user where (user=:user or qq=:user or mail=:user)limit 1', array(':user' => $user))){
                $url = "http://{$_SERVER['HTTP_HOST']}" . url('index/resetPassword', ['sid' => $row['sid']]);
                $body = "尊敬的用户：<br>您好，你申请找回密码。<br>请点击下面链接进行密码修改：<a href='{$url}' target='_blank'>{$url}</a>";
                $mail = sendEmail($row['mail'], config('web_name') . " - 找回密码", $body);
                if ($mail->send()) {
                    $this->assign('sweetAlert',sweetAlert('发送成功','激活邮件已发送至你邮箱，请查看！','1',url('index/login')));
                } else {
                    $this->assign('sweetAlert',sweetAlert('发送失败','邮件发送失败：' . $mail->ErrorInfo,'2','REFERER'));
                }
            }
        }
        unset($_SESSION['vc_code']);
        exit($this->fetch('common/sweetalert'));
    }
    public function logout()
    {
        Cookie::clear('smalls_');
        $this->assign('sweetAlert',sweetAlert('退出登录','退出登录成功，正在返回登录页面！','1',url('user/login')));
        exit($this->fetch('common/sweetalert'));
    }
}